package com.tfswx.fljdmxyy.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newbie.core.security.authorize.AuthorizationProvider;
import com.tfswx.fljdmxyy.dao.QxXtJkdyMapper;
import com.tfswx.fljdmxyy.dao.ZzjgXtJsMapper;
import com.tfswx.fljdmxyy.dao.ZzjgXtRyMapper;
import com.tfswx.fljdmxyy.model.QxXtJkdy;
import com.tfswx.fljdmxyy.model.ZzjgXtJs;
import com.tfswx.fljdmxyy.model.ZzjgXtRy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录用户权限认证
 *
 * @author 陈文佳
 * @date 2023/3/16
 **/
@Component
public class AuthorizationProviderConfig implements AuthorizationProvider {

    @Resource
    private ZzjgXtRyMapper zzjgXtRyMapper;

    @Resource
    private ZzjgXtJsMapper zzjgXtJsMapper;

    @Resource
    private QxXtJkdyMapper jkdyMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return null;
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return null;
    }

    /**
     * 返回一个账号所拥有的URLS集合
     */
    @Override
    public List<String> getUrlPatternList(Object loginId, String loginType) {
        // 1.拿到人员编码
        String rybm = loginId.toString();

        // 2.查询获取角色信息
        ZzjgXtRy ryxx = new ZzjgXtRy();
        ryxx.setRybm(rybm);
        ryxx = zzjgXtRyMapper.getZzjgXtRy(ryxx);
        if (StrUtil.isNotBlank(ryxx.getJsbm())) {
            ZzjgXtJs jsxx = new ZzjgXtJs();
            jsxx.setJsbm(ryxx.getJsbm());
            // 3.获取角拥有的功能编码
            jsxx = zzjgXtJsMapper.getZzjgXtJs(jsxx);
            List<QxXtJkdy> jkdyList;
            if (ObjectUtil.isNotNull(jsxx)) {
                if (StrUtil.isNotBlank(jsxx.getGnbm())) {
                    // 3.1 获取所有功能编码
                    List<String> gnbmList = Arrays.asList(jsxx.getGnbm().split(","));
                    // 3.2 查询所有功能定义
                    jkdyList = jkdyMapper.queryJkdz(gnbmList);
                    if (CollectionUtil.isNotEmpty(jkdyList)) {
                        return jkdyList.stream().map(QxXtJkdy::getJkdz).collect(Collectors.toList());
                    }
                }
            }
        }
        return null;
    }
}
